#ifndef SALES_DATA_H
#define SALES_DATA_H

#include <iostream>
#include <string>

using namespace std;

struct Sales_data {
  string bookNo;
  unsigned units_sold = 0;
  double revenue = 0.0;

  string isbn() const { return bookNo; }
  Sales_data& combine(const Sales_data&);
  double avg_price() const;

  void CalcRevenue(double price);
  double CalcAveragePrice();
  void SetData(Sales_data data);
  void AddData(Sales_data data);
  void Print();
};

// assumes that both objects refer to the same ISBN
Sales_data& Sales_data::combine(const Sales_data& data) {
    units_sold += units_sold + data.units_sold;
    revenue += revenue + data.revenue;
    return *this;
}

double Sales_data::avg_price() const {
    if (units_sold)
        return revenue / units_sold;
    else
        return 0;
}

void Sales_data::CalcRevenue(double price) { revenue = units_sold * price; }

void Sales_data::SetData(Sales_data data) {
  bookNo = data.bookNo;
  units_sold = data.units_sold;
  revenue = data.revenue;
}

void Sales_data::AddData(Sales_data data) {
  if (bookNo != data.bookNo) return;
  units_sold += data.units_sold;
  revenue += data.revenue;
}

double Sales_data::CalcAveragePrice() {
  if (units_sold != 0) {
    return revenue / units_sold;
  } else {
    return 0.0;
  }
}

void Sales_data::Print() {
  std::cout << bookNo << " " << units_sold << " " << revenue << " ";
  double averagePrice = CalcAveragePrice();
  if (averagePrice != 0.0) {
    std::cout << averagePrice << std::endl;
  } else {
    std::cout << "(no sales)" << std::endl;
  }
}
#endif
